package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotMessage;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotMessageDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2020-11-20 11:14:59
 */
public class IotMessageConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotMessageConverts.class);

    public static List<IotMessageDTO> convert(List<IotMessage> iotMessageList) {
        if (CollectionUtils.isEmpty(iotMessageList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotMessageDTO> iotMessageListResult = Lists.newArrayList();
        for (IotMessage iotMessage : iotMessageList) {
            iotMessageListResult.add(convert(iotMessage));
        }
        return iotMessageListResult;
    }

    public static IotMessageDTO convert(IotMessage iotMessage) {
        try {
            return BeanUtils.copy(iotMessage, IotMessageDTO.class);
        } catch (Exception e) {
            LOG.error("IotMessage到IotMessageDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotMessage到IotMessageDTO转换失败", e);
        }
    }

    public static IotMessage convert(IotMessageDTO iotMessageDTO) {
        try {
            return BeanUtils.copy(iotMessageDTO, IotMessage.class);
        } catch (Exception e) {
            LOG.error("IotMessageDTO到IotMessage转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotMessageDTO到IotMessage转换失败", e);
        }
    }
}